using System;
using System.Collections.Generic;

namespace GGameFramework.Core.FSM
{
    /// <summary>
    /// 状态机配置验证器。
    /// </summary>
    /// <typeparam name="T">状态机持有者类型。</typeparam>
    public class FsmConfiguration<T> where T : class
    {
        private readonly Dictionary<Type, HashSet<Type>> m_ValidTransitions;
        private readonly Dictionary<Type, string> m_StateNames;

        /// <summary>
        /// 初始化状态机配置验证器的新实例。
        /// </summary>
        public FsmConfiguration()
        {
            m_ValidTransitions = new Dictionary<Type, HashSet<Type>>();
            m_StateNames = new Dictionary<Type, string>();
        }

        /// <summary>
        /// 添加有效的状态转换。
        /// </summary>
        /// <typeparam name="TFromState">起始状态类型。</typeparam>
        /// <typeparam name="TToState">目标状态类型。</typeparam>
        public void AddValidTransition<TFromState, TToState>() 
            where TFromState : FsmState<T> 
            where TToState : FsmState<T>
        {
            Type fromStateType = typeof(TFromState);
            Type toStateType = typeof(TToState);

            if (!m_ValidTransitions.ContainsKey(fromStateType))
            {
                m_ValidTransitions[fromStateType] = new HashSet<Type>();
            }

            m_ValidTransitions[fromStateType].Add(toStateType);
        }

        /// <summary>
        /// 添加状态名称。
        /// </summary>
        /// <typeparam name="TState">状态类型。</typeparam>
        /// <param name="name">状态名称。</param>
        public void AddStateName<TState>(string name) where TState : FsmState<T>
        {
            m_StateNames[typeof(TState)] = name;
        }

        /// <summary>
        /// 检查状态转换是否有效。
        /// </summary>
        /// <param name="fromStateType">起始状态类型。</param>
        /// <param name="toStateType">目标状态类型。</param>
        /// <returns>状态转换是否有效。</returns>
        public bool IsValidTransition(Type fromStateType, Type toStateType)
        {
            if (fromStateType == null || toStateType == null)
            {
                return false;
            }

            // 如果没有配置转换规则，则允许所有转换
            if (m_ValidTransitions.Count == 0)
            {
                return true;
            }

            if (m_ValidTransitions.TryGetValue(fromStateType, out HashSet<Type> validTransitions))
            {
                return validTransitions.Contains(toStateType);
            }

            return false;
        }

        /// <summary>
        /// 获取状态名称。
        /// </summary>
        /// <param name="stateType">状态类型。</param>
        /// <returns>状态名称。</returns>
        public string GetStateName(Type stateType)
        {
            if (m_StateNames.TryGetValue(stateType, out string name))
            {
                return name;
            }
            return stateType.Name;
        }

        /// <summary>
        /// 获取所有有效的状态转换。
        /// </summary>
        /// <returns>所有有效的状态转换。</returns>
        public Dictionary<Type, HashSet<Type>> GetAllValidTransitions()
        {
            return new Dictionary<Type, HashSet<Type>>(m_ValidTransitions);
        }

        /// <summary>
        /// 清除所有配置。
        /// </summary>
        public void Clear()
        {
            m_ValidTransitions.Clear();
            m_StateNames.Clear();
        }
    }
}
